<?php


namespace thirdsdk;

/**
 * Http 请求类
 */
class Http
{
    /**
     * 平台
     *
     * @var string
     */
    protected $platform = '';

    /**
     * 构造函数
     *
     * @param  string $platform 平台
     */
    public function __construct($platform)
    {
        $this->platform = $platform;
    }

    /**
     * GET 请求
     *
     * @param string $url    请求地址
     * @param array  $header 请求头部
     *
     * @return array
     */
    public function get($url, $header = [])
    {
        $header = array_merge($header, ['Content-type:application/json', 'Accept:application/json']);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $res = curl_exec($curl);
        curl_close($curl);
        $res = json_decode($res, true);
        $this->response($res);

        return $res;
    }

    /**
     * POST 请求
     *
     * @param string $url    请求地址
     * @param array  $param  请求参数
     * @param array  $header 请求头部
     *
     * @return array
     */
    public function post($url, $param = [], $header = [])
    {
        if(count($header)<=0){
            $header = array_merge($header, ['Content-type:application/json;charset=utf-8', 'Accept:application/json']);
        }
        // 检查 headers 数组中是否包含 'application/x-www-form-urlencoded'
        $containsFormUrlEncoded = false;
        foreach ($header as $val) {
            if (strpos($val, 'application/x-www-form-urlencoded') !== false) {
                $containsFormUrlEncoded = true;
                break;
            }
        }
        if($containsFormUrlEncoded){
            $param = http_build_query($param);
        }else{
            $param = json_encode($param);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        $res = json_decode($res, true);
        $this->response($res);

        return $res;
    }

    /**
     * 响应拦截
     *
     * @param  array $res
     *
     * @return Exception|void
     */
    public function response($res)
    {
        if ($this->platform == 'wx') {
            $errcode = $res['ret'] ?? $res['code'] ?? $res['errcode'] ?? '';
            $errdesc = $res['msg'] ?? $res['errmsg'] ?? 'wxsdk error';
        }else if ($this->platform == 'qq') {
            $errcode = $res['ret'] ?? $res['code'] ?? $res['errcode'] ?? '';
            $errdesc = $res['msg'] ?? $res['errmsg'] ?? 'qqsdk error';
        }else if ($this->platform == 'wb') {
            $errcode = $res['error'] ?? $res['error_code'] ?? '';
            $errdesc = $res['error_description'] ?? 'wbsdk error';
        }else if ($this->platform == 'aliIdCard') {
            if(isset($res['success']) && $res['success']==true){
            }else{
                throw new \think\Exception("识别失败，请重试");
            }
        }else if ($this->platform == 'ali3ys') {
            if(isset($res['code']) && $res['code']=='00000'){
            }else{
                throw new \think\Exception(isset($res['message'])?$res['message']:"识别失败，请重试");
            }
        }else if ($this->platform == 'bankCard') {
            if(isset($res['success']) && $res['success']==true){
            }else{
                throw new \think\Exception("识别失败，请重试");
            }
        }else if ($this->platform == 'vrabbitInit') {
            if(array_key_exists('code', $res) && $res['code']==0){
            }else{
                throw new \think\Exception($res['msg']);
            }
        }else if ($this->platform == 'vrabbitSearchPlus') {
            if (array_key_exists('code', $res) && $res['code']==0 && strpos($res['msg'], '旺旺号不存在') != false) {
                // 执行相应的操作，比如抛出异常
                throw new \think\Exception('账号不存在');
            }else if(array_key_exists('code', $res) && $res['code']==0 && $res['msg']){
            }else{
                throw new \think\Exception($res['msg']);
            }
        }else if ($this->platform == 'taoceSearchPlus') {
            if(isset($res['success']) && $res['success']==true && isset($res['data']) && $res['data']){
            }else{
                $msg = "验号失败,请稍后重试";
                if(isset($res['info']) && $res['info']){
                    $msg =$res['info'];
                }else if(isset($res['msg']) && $res['msg']){
                    $msg =$res['msg'];
                }
                throw new \think\Exception($msg);
            }
        }else if ($this->platform == 'goodsItem') {
            if(array_key_exists('code', $res) && $res['code']==1){
            }else{
                $msg = "系统繁忙，请稍后再试";
                if(isset($res['msg']) && $res['msg']){
                    $msg =$res['msg'];
                }
                throw new \think\Exception($msg);
            }
        }else if ($this->platform == 'submitOrder') {
            if(array_key_exists('code', $res) && $res['code']==200){
            }else{
                $msg = "系统繁忙，请稍后再试";
                if(isset($res['msg']) && $res['msg']){
                    $msg =$res['msg'];
                }
                throw new \think\Exception($msg);
            }
        }
        if ($errcode ?? '') {
            throw new \think\Exception($errcode . ' ' . $errdesc, 400);
        }
    }
}
